﻿using SqlSugar;

namespace ConsoleframeORM
{
    internal class Program
    {
        static void Main(string[] args)
        {

            SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Server=127.0.0.1;Initial Catalog=test;User ID=sa;Password=123456",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true
            });


            Db.DbFirst
            //类
            .SettingClassTemplate(old => { return old;/*修改old值替换*/ })
            //类构造函数
            .SettingConstructorTemplate(old => { return old;/*修改old值替换*/ })
             .SettingNamespaceTemplate(old =>
             {
                 return old + "\r\nusing SqlSugar;"; //追加引用SqlSugar
             })
            //属性备注
            .SettingPropertyDescriptionTemplate(old => { return old;/*修改old值替换*/})

            //属性:新重载 完全自定义用配置
            .SettingPropertyTemplate((columns, temp, type) =>
            {

                var columnattribute = "\r\n           [SugarColumn({0})]";
                List<string> attributes = new List<string>();
                // 如果字段类型是bigint，添加JsonConverter特性
                string jsonConverterAttribute = "";
                if (columns.DataType == "bigint")
                {
                    jsonConverterAttribute = "\r\n           [Newtonsoft.Json.JsonConverter(typeof(ValueToStringConverter))]";
                }
                if (columns.IsPrimarykey)
                    attributes.Add("IsPrimaryKey=true");
                if (columns.IsIdentity)
                    attributes.Add("IsIdentity=true");

                if (attributes.Count == 0)
                {
                    columnattribute = "";
                }
                // 生成SugarColumn特性的字符串
                string sugarColumnAttributes = string.Join(",", attributes);
                string sugarColumnAttribute = string.IsNullOrEmpty(sugarColumnAttributes) ? "" : string.Format(columnattribute, sugarColumnAttributes);

                // 替换模板中的属性定义
                return jsonConverterAttribute + temp.Replace("{PropertyType}", type)
                                                    .Replace("{PropertyName}", columns.DbColumnName)
                                                    .Replace("{SugarColumn}", sugarColumnAttribute);
            })
            .Where("BOM").SettingClassDescriptionTemplate(it =>
            {
                return it + "\r\n    [Tenant(\"" + (Db.CurrentConnectionConfig.ConfigId ?? 0).ToString() + "\")]";
            })
           .IsCreateDefaultValue()
           .CreateClassFile("D:\\demo\\simpleORMframe\\ConsoleframeORM\\SYSModel");
        }
    }
}
